// Package api
// @Author: zhangdi
package sysctrl

import (
	"encoding/json"
	"server/application/constant/enums"
	"server/application/constant/syscnt"
	"server/application/db/system/columns"
	"server/application/db/system/dbdic"
	"server/application/db/system/dbdto"
	"server/application/db/system/dbsvc"
	"server/application/db/system/repository"
	"server/application/dto"
	"server/pkg/ginp"
	"server/pkg/where"
	"strings"
)

func SysDictionaryCreate(ctx *ginp.ContextPro) {
	var params *dbdto.SysDictionaryCreate
	if err := ctx.ShouldBindJSON(&params); err != nil {
		ctx.ErrJson("请求参数有误" + err.Error())
		return
	}
	info, err := dbsvc.SysDictionaryCreate(params)
	if err != nil {
		ctx.ErrJson("创建失败" + err.Error())
		return
	}
	ctx.OkJsonData(info)
}

func SysDictionaryUpdate(ctx *ginp.ContextPro) {
	var params *dbdto.SysDictionaryUpdate
	if err := ctx.ShouldBindJSON(&params); err != nil {
		ctx.ErrJson("请求参数有误" + err.Error())
		return
	}
	wheres := where.Format(where.OptEqual("id", params.ID))
	err := dbsvc.SysDictionaryUpdate(wheres, params, repository.UpdateColumnsAll)
	if err != nil {
		ctx.ErrJson("修改失败" + err.Error())
		return
	}
	ctx.OkJson()
}

func SysDictionaryDelete(ctx *ginp.ContextPro) {
	var params *dbdto.SysDictionaryDelete
	if err := ctx.ShouldBindJSON(&params); err != nil {
		ctx.ErrJson("请求参数有误" + err.Error())
		return
	}

	err := dbsvc.SysDictionaryDelete(where.FormatOne("id", "IN", params.ID))
	if err != nil {
		ctx.ErrJson("删除失败" + err.Error())
		return
	}
	ctx.OkJson()
	return
}
func SysDictionarySearch(ctx *ginp.ContextPro) {
	var params *dto.ReqSearch
	if err := ctx.ShouldBindJSON(&params); err != nil {
		ctx.ErrJson("请求参数有误" + err.Error())
		return
	}
	list, total := dbsvc.SysDictionarySearch(params.Wheres, params.Extra)

	resp := &dto.RespSearch{
		List:     list,
		Total:    uint(total),
		PageNum:  uint(params.Extra.PageNum),
		PageSize: uint(params.Extra.PageSize),
	}
	ctx.OkJsonData(resp)
}

func SysDictionaryGetOptions(ctx *ginp.ContextPro) {
	var params *dto.ReqGetDicOptions
	if err := ctx.ShouldBindJSON(&params); err != nil {
		ctx.ErrJson("请求参数有误" + err.Error())
		return
	}

	info, err := dbsvc.SysDictionaryFindOne(where.FormatOne(columns.SysDictionaryDicKey, "=", params.DicKeyName))
	if err != nil {
		ctx.ErrJson("查询异常" + err.Error())
		return
	}
	if info.ID <= 0 {
		ctx.ErrJson("查询字典结果为空")
		return
	}
	var dicData []dbdic.DicOption
	//自定义键值对options
	if info.DicValueType == enums.DicValueTypeOptions {
		err := json.Unmarshal([]byte(info.DicValue), &dicData)
		if err != nil {
			ctx.ErrJson("options json数据解析异常" + err.Error())
			return
		}
	}

	//通过sql查询
	if info.DicValueType == enums.DicValueTypeSqlSelect {
		dicData, _ = dbsvc.SysDictionaryGetOptionsBySql(info.DicValue)
	}
	//通过sql远程搜索查询
	if info.DicValueType == enums.DicValueTypeSqlSearch {
		if params.SearchText == "" {
			ctx.ErrJson("远程搜索search text参数不能为空")
			return
		}
		//先替换再搜索
		sql := strings.ReplaceAll(info.DicValue, syscnt.SysOptionsSearchText, params.SearchText)
		dicData, _ = dbsvc.SysDictionaryGetOptionsBySql(sql)
	}

	//值默认是
	options := dbsvc.SysDictionaryFilter(dicData, info.ValueDataType)

	ctx.OkJsonData(options)
}

// SysDictionaryGetString 获取字典值
func SysDictionaryGetString(ctx *ginp.ContextPro) {
	var params *dto.ReqSysDictionaryGetString
	if err := ctx.ShouldBindJSON(&params); err != nil {
		ctx.ErrJson("请求参数有误" + err.Error())
		return
	}
	val := dbsvc.SysDictionaryGetString(params.DicKeyName)
	ctx.OkJsonData(val)
}

// SysDictionarySetString 设置字典string类型值
func SysDictionarySetString(ctx *ginp.ContextPro) {
	var params *dto.ReqSysDictionarySetString
	if err := ctx.ShouldBindJSON(&params); err != nil {
		ctx.ErrJson("请求参数有误" + err.Error())
		return
	}
	res := dbsvc.SysDictionarySetString(params.DicKeyName, params.DicKeyValue)
	ctx.OkJsonData(res)
}
